#!/usr/bin/env bash

################################################################################
# Runs bazel build for proto files in cirq/google/api/* that have changed.
#
# If any BUILD file for the proto has changed, all targets in that BUILD
# file will be built.
#
# Usage:
#     check/build-changed-protos [BASE_REVISION]
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a line is considered to have "changed"). For
# example, you can compare against 'origin/master' or 'HEAD~1'.
#
# If you don't specify a base revision, the following defaults will be tried, in
# order, until one exists:
#
#     1. upstream/master
#     2. origin/master
#     3. master
#
# If none exists, the script fails.
#
# See build-protos for building all protos.
################################################################################

# Get the working directory to the repo root.
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$(git rev-parse --show-toplevel)"

# Figure out which revision to compare against.
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
    if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
        echo -e "\033[31mNo revision '$1'.\033[0m" >&2
        exit 1
    fi
    rev=$1
elif [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
    rev=upstream/master
elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
    rev=origin/master
elif [ "$(git cat-file -t master 2> /dev/null)" == "commit" ]; then
    rev=master
else
    echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/master' or 'HEAD~1').\033[0m" >&2
    exit 1
fi
base="$(git merge-base ${rev} HEAD)"
if [ "$(git rev-parse ${rev})" == "${base}" ]; then
    echo -e "Comparing against revision '${rev}'." >&2
else
    echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
    rev="${base}"
fi

# All the protos.
protos=$(find -type f -name "*.proto" -printf '%P\n' \
    | grep "google/api/.*\.proto$" \
    | sed -e "s/\.proto$//")

# Get the changed protos and strip them of ".proto"
changed=$(git diff --name-only ${rev} -- \
    | grep "google/api/.*\.proto$" \
    | sed -e "s/\.proto$//"
)
if [[ ! -z "$changed" ]]; then
    for base in $changed:
    do
        echo -e "Found changed proto: '${base}.proto'"
    done
fi

# Check if any of the build files have changed.
build_changed=$(git diff --name-only ${rev} -- \
    | grep "google/api/.*BUILD$" \
    | sed -e "s/BUILD//")

if [[ -z "$build_changed" ]]; then
    echo -e "No BUILD files changed."
else
    # Build all the targets for the changed BUILD rule.
    echo -e "BUILD file changed in directories: '${build_changed[@]}'"
    for build_prefix in $build_changed
    do
        (set -x; bazel build "${build_prefix}...:*")
    done
fi

# We potentially will attempt to build a second time, but this is not
# a problem as bazel returns immediately.
for base in $changed
do
    (set -x; bazel build "${base}_proto"; \
    bazel build "${base}_py_proto"; \
    bazel build "${base}_cc_proto")
done


